package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
import com.taotao.pojo.TbContentExample.Criteria;
import com.taotao.service.ContentService;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ContentServiceImpl implements ContentService {
	@Autowired
	private TbContentMapper tbContentMapper;

	@Override
	public EasyUIDataGridResult getContentList(long categoryId, int page,
			int rows) {
		PageHelper.startPage(page, rows);

		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(categoryId);
		List<TbContent> list = tbContentMapper
				.selectByExampleWithBLOBs(example);

		PageInfo<TbContent> info = new PageInfo<TbContent>(list);

		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setTotal(info.getTotal());
		result.setRows(info.getList());

		return result;
	}

	@Override
	public TaotaoResult insertContent(TbContent tbContent) {
		tbContent.setCreated(new Date());
		tbContent.setUpdated(new Date());

		tbContentMapper.insert(tbContent);
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult updateContent(TbContent tbContent) {
		tbContent.setUpdated(new Date());
		tbContentMapper.updateByPrimaryKeySelective(tbContent);
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult deleteContent(long[] ids) {
		for (long l : ids) {
			tbContentMapper.deleteByPrimaryKey(l);
		}
		return TaotaoResult.ok();
	}

	@Override
	public TbContent getTbContentById(long id) {
		return tbContentMapper.selectByPrimaryKey(id);

	}
}
